<template>
	<view class="container">
		<view v-if="videoList.length == 0" class="no-content">
			暂无视频，快上传您的视频吧
		</view>
		<view v-else>
			<view class="edit-container">
				<view class="edit-container-all" @click="clickAll" v-if="isEdit">全选</view>
				<view class="edit-container-notall" @click="clickNotall" v-if="isEdit">全不选</view>
				<view class="edit-container-delete" @click="deleteVideos" v-if="isEdit">删除</view>
				<view class="edit-container-edit" @click="editVideo">{{isEdit?'完成':'编辑'}}</view>
			</view>
			<view style="display: flex;flex-wrap: wrap;margin-top: 60rpx;">
				<view class="photo-item" v-for="(item,index1) in videoList" :key="index1">
					<video class="photo-item-img" v-if="item.MediaUrl !=null && item.MediaUrl.length>0" :src="item.MediaUrl"></video>
					<view style="display: flex;justify-items: center;">
						<view class="delete-container" @click="selectedVideo(index1)" v-if="isEdit">
							<image class="delete-container-icon" :src="selList.indexOf(index1) != -1?sel_icon:normal_icon"></image>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="add-btn bottom-change-view" @click="addVideo" v-if="!isEdit">上传视频</view>
	</view>
</template>

<script>
	import normal_icon from '@/static/images/dui@3x.png'
	import sel_icon from '@/static/images/dui_1@3x.png'
	import {
		isLogin,
		toLogin,
		successToShow,
		errorToShow
	} from '@/config/common.js'
	import { uploadVideoToQiNiu } from '@/constant/home.js'
	import {
		getServePlayerVideoInfoList
	} from '@/constant/customer.js'
	import {
		addServePlayerMediaInfo,
		deleteServeMedia
	} from '@/constant/schedule.js'
	export default {
		components: {

		},
		data() {
			return {
				normal_icon: normal_icon,
				sel_icon: sel_icon,
				selList: [],
				isEdit: false,
				videoList: [],
				videos: [],
				query_filter: {
					servePlayerId: '',
					customerId: uni.getStorageSync('customerId'),
					pageIndex: 0,
					pageSize: 10,
				},
				servePlayerId: uni.getStorageSync('customerId')
			}
		},
		onLoad: function(option) {
			if (isLogin()) {
				if (option.servePlayerId) {
					this.servePlayerId = option.servePlayerId;
					this.query_filter.servePlayerId = option.servePlayerId;
					if (this.servePlayerId) {
						this.getVideoList(this.query_filter);
					}
				}
			} else {
				toLogin();
			}
		},
		onReachBottom() {
			this.query_filter.pageIndex++;
			this.getVideoList(this.query_filter)
		},
		onPullDownRefresh() {
			this.query_filter.pageIndex = 0;
			this.getVideoList(this.query_filter)
		},
		methods: {
			deleteVideos() {
				if(this.selList.length == 0){
					errorToShow('未选中任何记录')
					return;
				}
				let that = this;
				uni.showModal({
					title: '',
					content: '确定删除吗',
					success: function(res) {
						if (res.confirm) {
							that.deleteServeVideo();
						}
					}
				});
			},
			clickAll() {
				this.videoList.forEach((vm, index) => {
					this.selList.push(index)
				})
			},
			clickNotall() {
				this.selList = [];
			},
			editVideo() {
				this.isEdit = !this.isEdit;
			},
			selectedVideo(val) {
				if (this.selList.indexOf(val) != -1) {
					this.selList.splice(this.selList.indexOf(val), 1)
				} else {
					this.selList.push(val)
				}
			},
			addVideo() {
				let that = this;
				uploadVideoToQiNiu(5, res => {
					that.addServePlayerVideo(res);
				});
			},
			async getVideoList() { //评论列表
				let res = await getServePlayerVideoInfoList(this.query_filter);
				uni.stopPullDownRefresh()
				if (this.query_filter.pageIndex > 0) {
					for (let i = 0; i < res.Source.length; i++) {
						let obj = res.Source[i];
						this.videoList.push(obj);
					}
				} else {
					this.videoList = res.Source;
				}
				this.videos = [];
				for (let i = 0; i < this.videoList.length; i++) {
					this.videos.push(this.videoList[i].MediaUrl);
				}
			},
			async addServePlayerVideo(mediaUrl) { //上传照片
				if(mediaUrl != null){
					let res = await addServePlayerMediaInfo({ServePlayerId:this.servePlayerId,MediaUrl:mediaUrl,MediaType:1});
					successToShow('上传成功',res => {
						this.getVideoList();
					})
				}
			},
			async deleteServeVideo() { //删除照片
				let ids = [];
				let that = this;
				that.selList.forEach(function(item){
					ids.push(that.videoList[item].Id);
				});
				let res = await deleteServeMedia({ListIds:ids});
				successToShow('删除成功',res => {
					that.selList = [];
					that.isEdit = false;
					that.getVideoList();
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.photo-item {
		width: 30%;
		margin-left: 2.5%;
		margin-bottom: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: flex-end;
		position: relative;

		&-img {
			width: 100%;
			height: 260rpx;
			margin-bottom: 20rpx;
		}

		&-icon {
			width: 20rpx;
			height: 20rpx;
		}
	}

	.delete-container {
		display: flex;
		align-items: center;
		font-size: 26rpx;
		position: absolute;
		right: 4rpx;
		top: 8rpx;
		color: #fff;

		&-icon {
			width: 48rpx;
			height: 48rpx;
			margin-right: 10rpx;
		}
	}

	.no-content {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 100vh;
	}

	.add-btn {
		position: fixed;
		bottom: 20rpx;
		width: 670rpx;
		left: 40rpx;
		height: 80rpx;
		border-radius: 40rpx;
		background-color: $main-theme-color-green;
		color: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.edit-container {
		display: flex;
		align-items: center;
		margin: 20rpx;

		&-all {
			background-color: #007AFF;
			width: 120rpx;
			color: #fff;
			height: 50rpx;
			border-radius: 25rpx;
			font-size: 28rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		&-notall {
			background-color: #67C23A;
			width: 120rpx;
			color: #fff;
			height: 50rpx;
			border-radius: 25rpx;
			font-size: 28rpx;
			margin-left: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		&-edit {
			background-color: #007AFF;
			width: 120rpx;
			color: #fff;
			height: 50rpx;
			border-radius: 25rpx;
			font-size: 28rpx;
			margin-left: auto;
			display: flex;
			align-items: center;
			justify-content: center;
		}

		&-delete {
			background-color: #FF0000;
			width: 120rpx;
			color: #fff;
			height: 50rpx;
			border-radius: 25rpx;
			font-size: 28rpx;
			margin-left: 20rpx;
			display: flex;
			align-items: center;
			justify-content: center;
		}
	}
</style>
